
import os
import uuid
import time
from crudtool import GenericCrudTool
from bip44 import Wallet
from mnemonic import Mnemonic
import random
import hashlib
english_wordlist = Mnemonic("english").wordlist
script_dir = os.path.dirname(os.path.abspath(__file__))



tool=GenericCrudTool(host="127.0.0.1",user="root",password="root",db="coin_account")


def generate_random_words(word_list):
    return random.sample(word_list, 12)


def create_tags():
    tags=generate_random_words(english_wordlist)
    return tags

def make_shuffle(tags):
    words=tags.copy()
    random.shuffle(words)
    return words


def get_tron_private_key(mnemonic: str) -> str:
    # 生成符合BIP44标准的TRON路径
    wallet = Wallet(mnemonic)
    # TRON专用路径 m/44'/195'/0'/0/0
    private_key, _ = wallet.derive_account(195, account=0, change=0, address_index=0)
    return private_key.hex()

def get_md5(text):
    md5 = hashlib.md5()
    md5.update(text.encode('utf-8'))
    return md5.hexdigest()  # 自动返回32位小写

def do_work():
    tags=create_tags()
    save_one(tags)
    tags1=make_shuffle(tags)
    save_one(tags1)
    tags2=make_shuffle(tags)
    save_one(tags2)
    tags3=make_shuffle(tags)
    save_one(tags3)


def save_one(tags):
    tags_str=" ".join(tags)
    md5=get_md5(tags_str)
    private_key=get_tron_private_key(tags_str)
    save_data(tags_str,md5,private_key,"tron")

def save_data(tags_str,md5_val,private_key,category):
    try:
        tool.insert("tb_tag_fix",{"name":tags_str,"md5_val":md5_val,"private_key":private_key,"category":category})
    except Exception as e:
        pass
    

if __name__ == "__main__":
    print("start")
    for i in range(1000000):
        print(i)
        try:
            do_work()
        except Exception as e:
            print(e)
    
    print("done")
